﻿using SIE.SCADA.Common;
using SIE.SCADA.Common.Command;
using SIE.SCADA.Common.Object;
using SIE.SCADA.SuperSocket.Rpc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIE.SCADA.Common.Log;
using Models;
using Model.Core;
using SIE.SCADA.ZemtDemo.Common;

namespace SIE.SCADA.ZemtDemo
{
    class ZemtDemoCommand : BaseObject, IBaseCommand
    {
        private const string _deviceName = "Tag.ScanDevice";

        public ZemtDemoCommand()
        {
            Name = "ZemtDemoCommand";
        }

        private bool Init()
        {
            return true;
        }

        /// <summary>
        /// 应答：扫描枪信息
        /// <returns></returns>
        public Response<object> GetOrderMsg(string stationid, string scanMsg)
        {
            Response<object> response = new Response<object>();
            try
            {
                //Logger.Info($"设备数量：{Env.Machines.Count}");
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    var orderMsg = torque.RequestWoInfo(scanMsg);
                    response.Data = orderMsg;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                
            }
            return response;
        }

        /// <summary>
        /// 应答：扭力扳手信息
        /// </summary>
        /// <param name="stationid"></param>
        /// <param name="v_Scada_Jobsteprecords"></param>
        /// <returns></returns>
        public Response<object> UpdateWorkState(string stationid, List<scada_jobstep_record> jobsteps)
        {
            Response<object> response = new Response<object>();
            try
            {
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    if (jobsteps.Any())
                    {
                        torque.Update_Process(jobsteps);
                        response.Data = true;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Data = false;
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
            }
            return response;
        }

        /// <summary>
        /// 工步信息缓存恢复
        /// </summary>
        /// <param name="stationid"></param>
        /// <param name="jobsteps"></param>
        /// <returns></returns>
        public Response<object> RecoveryState(string stationid, List<scada_jobstep_record> jobsteps)
        {
            Response<object> response = new Response<object>();
            try
            {
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    if (jobsteps.Any())
                    {
                        torque.Update_Process(jobsteps);
                        response.Data = true;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Data = false;
                Logger.Warn(ex.Message);
                Logger.Warn(ex.StackTrace);
            }
            return response;
        }

        /// <summary>
        /// KJF 20210323 新增
        /// 扭力扳手信息缓存恢复
        /// </summary>
        /// <returns></returns>
        public Response<object> RecoveryTorqueRecord(string stationid, List<TorqueParmas> TorqueParams)
        {
            Response<object> response = new Response<object>();
            try
            {
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    if (TorqueParams.Any())
                    {
                        torque.TorqueRecordRecovery(TorqueParams);
                        response.Data = true;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Data = false;
                Logger.Warn(ex.Message);
                Logger.Warn(ex.StackTrace);
            }
            return response;
        }

        

        /// <summary>
        /// 获取当前子工序信息
        /// </summary>
        /// <param name="stationid"></param>
        /// <param name="scanMsg"></param>
        /// <returns></returns>
        public Response<object> GetCurSubProcess(string stationid)
        {
            Response<object> response = new Response<object>();
            try
            {
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    var orderMsg = torque.GetCurSubProcess();
                    response.Data = orderMsg;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
                Logger.Warn(ex.StackTrace);
            }
            return response;
        }

        /// <summary>
        /// 补打历史的一枪的接口
        /// </summary>
        /// <returns></returns>
        public Response<object> GetRetryRecordStep(string stationid)
        {
            Response<object> response = new Response<object>();
            try
            {
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    var orderMsg = torque.GetRetryStep();
                    response.Data = orderMsg;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
            }
            return response;
        }

        
        /// <summary>
        /// 应答：心跳信号
        /// </summary>
        /// <param name="stationid"></param>
        /// <returns></returns>
        public Response<object> Heartbeat(string stationid)
        {
            Response<object> response = new Response<object>();
            if(Env.Machines.TryGetValue(stationid, out var machine))
            {
               

                var torque = machine as ViewModel.TorqueWrench;
                 torque.Heartbeat();
                response.Data = true;



            }
            return response;
        }

        /// <summary>
        /// 接收：客户端信息
        /// </summary>
        /// <param name="stationid"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Response<object> RecvMsg(string stationid,string msg)
        {
            Response<object> response = new Response<object>();
            if (Env.Machines.TryGetValue(stationid, out var machine))
            {
                var torque = machine as ViewModel.TorqueWrench;
                torque.ClientMsg +=DateTime.Now.ToString()+ "："+msg+"；"+"\r";
                response.Data = true;
            }
            else
            {
                response.Data = false;
            }
            return response;
        }

        /// <summary>
        /// 重打
        /// </summary>
        /// <param name="stationid"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Response<object> ReWorkUpdateState(string stationid, string msg)
        {
            Response<object> response = new Response<object>();
            if (Env.Machines.TryGetValue(stationid, out var machine))
            {
                var torque = machine as ViewModel.TorqueWrench;
                torque.ClientMsg = msg;
                response.Data = true;
            }
            else
            {
                response.Data = false;
            }
            return response;
        }

        public Response<object> TroqueParmaRecord(string stationid, TorqueParmas torqueParmas)
        {
            Response<object> response = new Response<object>();

            try{
                if (Env.Machines.TryGetValue(stationid, out var machine))
                {
                    var torque = machine as ViewModel.TorqueWrench;
                    torque.TorqueRecord(torqueParmas);
                    response.Data = true;
                }
                else
                {
                    response.Data = false;
                }
            }catch (Exception  ex) {

                Logger.Error(ex.Message+""+ex.StackTrace);
            }


            return response;
        }
    }
}
